import 'dart:async';

import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_net/se_net.dart';
import 'package:squat_easy/se_net/se_net_builder.dart';
import 'package:squat_easy/se_net/se_net_skeleton.dart';
import 'package:squat_easy/se_service/se_user_service.dart';
import 'package:squat_easy/se_standard/se_app_colors.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeGlobal {
  static Timer? _timer;

  static void getAtt() async {
    _timer = Timer.periodic(const Duration(seconds: 2), (timer) {
      _getAtt();
    });
  }

  static void _getAtt() async {
    final TrackingStatus status = await AppTrackingTransparency.trackingAuthorizationStatus;
    if (status == TrackingStatus.notDetermined) {
      Future.delayed(const Duration(seconds: 2), () async {
        await AppTrackingTransparency.requestTrackingAuthorization();
      });
    } else {
      _timer?.cancel();
      _timer = null;
    }
  }

  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

    setSystemUi();

    ///配置UI
    YlmGlobalConfig().init(
      appBarConfig: YlmGlobalAppBarConfig(
        leading: Assets.svgAppBarBack.toSvg(width: 42),
      ),
      actionSheetConfig: YlmGlobalActionSheetGlobalConfig(
        cancelTitle: 'Cancel',
        confirmTitle: 'Confirm',
      ),
      assetPickerConfig: YlmGlobalPhotoPickerConfig(themeColor: SeAppColors.primary),
      loadingConfig: YlmGlobalLoadingConfig(color: SeAppColors.primary),
      permissionConfig: YlmGlobalPermissionConfig(
        photoTitle: 'Photo permission required',
        photoMessage: 'Please allow photo permission in settings',
        cameraTitle: 'Camera permission required',
        cameraMessage: 'Please allow camera permission in settings',
        microphoneTitle: 'Microphone permission required',
        microphoneMessage: 'Please allow microphone permission in settings',
        storageTitle: 'Storage permission required',
        storageMessage: 'Please allow storage permission in settings',
        cancelTitle: 'Cancel',
        confirmTitle: 'Confirm',
      ),
    );

    getAtt();

    Get.put(SeUserService());

    ///初始本地持久化
    await CommonStorage().init();

    ///初始化设备信息
    await SeDeviceInfoUtils().init();

    ///初始化包信息
    await SePackageInfoUtils().init();

    /// 初始化网络请求
    await SeNet().init(SeProductNetSkeleton(), SeProductNetBuilder());
  }

  // 系统样式
  static void setSystemUi() {
    // SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    //   statusBarColor: Colors.transparent,
    //   statusBarIconBrightness: Brightness.light,
    // ));

    if (GetPlatform.isMobile) {
      // 屏幕方向 竖直上
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    }

    if (GetPlatform.isAndroid) {
      // 去除顶部系统下拉和底部系统按键
      // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
      // 去掉底部系统按键
      // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      //     overlays: [SystemUiOverlay.bottom]);

      // 自定义样式
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        // 顶部状态栏颜色
        // statusBarColor: Colors.transparent,
        // 该属性仅用于 iOS 设备顶部状态栏亮度
        // statusBarBrightness: Brightness.light,
        // 顶部状态栏图标的亮度
        // statusBarIconBrightness: Brightness.light,

        // 底部状态栏与主内容分割线颜色
        systemNavigationBarDividerColor: Colors.transparent,
        // 底部状态栏颜色
        systemNavigationBarColor: Colors.white,
        // 底部状态栏图标样式
        systemNavigationBarIconBrightness: Brightness.dark,
      );
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  }
}
